package com.jp.fm.data;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.Date;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.math.NumberUtils;

public class Account implements Serializable, Comparable<Account> {

	private static final long serialVersionUID = 1L;

	public static final int DEFAULT_REFRESH_INTERVAL_MINUTES = 720;

	private String name = null;
	private Integer id = null;
	private Institution institution = null;
	private Long accountNumber = null;
	private AccountType type = null;
	private AccountBalance balance = null;
	private Float availableCredit = Float.valueOf(NumberUtils.FLOAT_ZERO);
	private Float totalCredit = Float.valueOf(NumberUtils.FLOAT_ZERO);
	private int sequence = 0;
	private Timestamp lastupdate = null;
	private Timestamp lastFailureDate = null;
	private boolean canRefresh = false;
	private boolean hidden = false;
	private int refreshInterval = DEFAULT_REFRESH_INTERVAL_MINUTES;

	public Account(final Integer id, final String name,
			final Institution institution, final Long accountNumber,
			final AccountType type, final AccountBalance balance,
			final Integer sequence) {
		super();
		this.id = id;
		this.name = name;
		this.institution = institution;
		this.accountNumber = accountNumber;
		this.type = type;
		this.balance = balance;
		this.sequence = sequence;
	}

	public final boolean isHidden() {
		return this.hidden;
	}

	public final void setHidden(final boolean hidden) {
		this.hidden = hidden;
	}

	public final Timestamp getLastFailureDate() {
		return this.lastFailureDate;
	}

	public final void setLastFailureDate(final Timestamp lastFailureDate) {
		this.lastFailureDate = lastFailureDate;
	}

	public static class AccountBalance implements Serializable {
		private static final long serialVersionUID = 1L;
		private Integer id = null;
		private Float balance = null;
		private Date lastUpdate = null;

		public AccountBalance(final Integer id, final Float balance,
				final Date lastUpdate) {
			super();
			this.id = id;
			this.balance = balance;
			this.lastUpdate = lastUpdate;
		}

		public final Integer getId() {
			return this.id;
		}

		public final Float getBalance() {
			return this.balance;
		}

		public final Date getLastUpdate() {
			return this.lastUpdate;
		}
	}

	public final AccountBalance getAccountBalance() {
		return this.balance;
	}

	public final Integer getSequence() {
		return sequence;
	}

	public final Float getAvailableCredit() {
		return this.availableCredit;
	}

	public final void setAvailableCredit(final Float availableCredit) {
		this.availableCredit = availableCredit;
	}

	public final Float getTotalCredit() {
		return this.totalCredit;
	}

	public final void setTotalCredit(final Float totalCredit) {
		this.totalCredit = totalCredit;
	}

	public final Timestamp getLastupdate() {
		return this.lastupdate;
	}

	public final void setLastupdate(final Timestamp lastupdate) {
		this.lastupdate = lastupdate;
	}

	public final Float getBalance() {
		return this.balance.getBalance();
	}

	public final void setBalance(final Float balance) {
		this.balance = new AccountBalance(null, balance, null);
	}

	public final AccountType getType() {
		return this.type;
	}

	public final void setType(final AccountType type) {
		this.type = type;
	}

	public final String getName() {
		return this.name;
	}

	public final void setName(final String name) {
		this.name = name;
	}

	public final Institution getInstitution() {
		return this.institution;
	}

	public final void setInstitution(final Institution institution) {
		this.institution = institution;
	}

	public final Long getAccountNumber() {
		return this.accountNumber;
	}

	public final void setAccountNumber(final Long accountNumber) {
		this.accountNumber = accountNumber;
	}

	public final boolean isCanRefresh() {
		return canRefresh;
	}

	public final void setCanRefresh(final boolean canRefresh) {
		this.canRefresh = canRefresh;
	}

	public final Integer getId() {
		return this.id;
	}

	public final int getRefreshInterval() {
		return this.refreshInterval;
	}

	public final void setRefreshInterval(final int refreshInterval) {
		this.refreshInterval = refreshInterval;
	}

	public final String toString() {
		return ToStringBuilder.reflectionToString(this);
	}

	@Override
	public final int compareTo(final Account o) {
		return this.getName().compareTo(o.getName());
	}
}
